package simbot.example.listener;

import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.annotation.OnPrivate;
import love.forte.simbot.api.message.MessageContent;
import love.forte.simbot.api.message.MessageContentBuilder;
import love.forte.simbot.api.message.MessageContentBuilderFactory;
import love.forte.simbot.api.message.containers.AccountInfo;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.filter.MatchType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import simbot.example.entity.Herosinformation;
import simbot.example.entity.Story;
import simbot.example.game.wuziqi;
import simbot.example.mapper.HerosinformationMapper;
import simbot.example.mapper.QuestionMapper;
import simbot.example.mapper.StoryMapper;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.Date;
import java.util.HashMap;

/**
 * 私聊消息监听的示例类。
 * 所有需要被管理的类都需要标注 {@link Service} 注解。
 *
 * 由于当前是处于springboot环境下，因此强烈建议类上的注释使用：
 * <ul>
 *     <li>{@link org.springframework.stereotype.Component}</li>
 *     <li>{@link Service}</li>
 * </ul>
 * 等注解来代替simbot的 {@link Beans}。
 *
 * 同样的，依赖注入也请使用 {@link Autowired} 等Springboot相关的注解。
 *
 * @author ForteScarlet
 */
@Service
public class MyPrivateListen {

    @Autowired
    private MessageContentBuilderFactory messageBuilderFactory;
    @Resource
    private StoryMapper storyMapper;
    @Resource
    private HerosinformationMapper herosinformationMapper;

    @Resource
    private wuziqi wzq;


    public Sender comSender;

    /**
     * log
     */
    private static final Logger LOG = LoggerFactory.getLogger(MyGroupListen.class);


    private String teacher;
    private String temporaryQuestion;
    private String temporaryAnswer;
    private String questionType;

    public static HashMap<String,int[][]> map = new HashMap<>();
    public static String playName = "";
    @OnPrivate
    @Filter(value = "五子棋", matchType = MatchType.EQUALS, trim = true)
    public void playWuZiQi(PrivateMsg groupMsg, Sender sender) {
        MessageContentBuilder builder = messageBuilderFactory.getMessageContentBuilder();
        MessageContent messageContent = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\棋盘.jpg").build();
        sender.sendPrivateMsg(groupMsg, messageContent);
        sender.sendPrivateMsg(groupMsg, "输入坐标落子");
        playName=groupMsg.getAccountInfo().getAccountCode();
        map.put(groupMsg.getAccountInfo().getAccountCode(),new int[15][15]);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(ImageIO.read(new File("D:\\steam\\steamapps\\common\\wuzi\\棋盘.jpg")),"JPG",outputStream);//将字节流进行后缀的修饰，并将图片放入字节流中
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        byte[] content = outputStream.toByteArray();//得到字节流中的所有字节
        try (BufferedOutputStream inputStream = new BufferedOutputStream(new FileOutputStream("D:\\steam\\steamapps\\common\\wuzi\\"+groupMsg.getAccountInfo().getAccountCode()+".jpg"))) {
            //将所传过来的字节流中的字节设置为输出流
            inputStream.write(content);//将字节写出到指定位置
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnPrivate
    public void luoZi(PrivateMsg groupMsg, Sender sender) {
        MessageContentBuilder builder = messageBuilderFactory.getMessageContentBuilder();
        String test = groupMsg.getText();
        if (groupMsg.getAccountInfo().getAccountCode().equals(playName)&&test.trim().length()==2){
            char[] chars = test.toCharArray();
            if (47<(int)chars[0]&&(int)chars[0]<58&&chars[1]-'0'<64&&chars[1]>48){
                wzq.setQiPan(map.get(groupMsg.getAccountInfo().getAccountCode()));
                if (map.get(groupMsg.getAccountInfo().getAccountCode())[(chars[1] - '0') - 49][(int) (chars[0]-'0')-1]==2){
                    sender.sendPrivateMsg(groupMsg,"这个点被下了，换个地方吧");
                    return;
                }
                boolean bai = wzq.bai((chars[1] - '0') - 49,(int) (chars[0]-'0')-1,  new File("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg"));
                map.put(groupMsg.getAccountInfo().getAccountCode(),wzq.qiPan);
                if (bai){
                    MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                    sender.sendPrivateMsg(groupMsg,msg);
                    sender.sendPrivateMsg(groupMsg,"你赢啦");
                    map.remove(groupMsg.getAccountInfo().getAccountCode());
                    return;
                }
                wzq.setQiPan(map.get(groupMsg.getAccountInfo().getAccountCode()));
                boolean hei = wzq.hei(new File("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg"));
                map.put(groupMsg.getAccountInfo().getAccountCode(),wzq.qiPan);
                if (hei){
                    MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                    sender.sendPrivateMsg(groupMsg,msg);
                    sender.sendPrivateMsg(groupMsg,"我赢啦");
                    return;
                }
                MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                sender.sendPrivateMsg(groupMsg,msg);
                sender.sendPrivateMsg(groupMsg,"请落子");
            }
        }
        if (groupMsg.getAccountInfo().getAccountCode().equals(playName)&&test.trim().length()==3){
            char[] chars = test.toCharArray();
            if ((int)chars[0]==49&&47<(int)chars[1]&&(int)chars[1]<58&&chars[2]-'0'<64&&chars[2]>48){
                String s ="";
                wzq.setQiPan(map.get(groupMsg.getAccountInfo().getAccountCode()));
                if (map.get(groupMsg.getAccountInfo().getAccountCode())[(chars[2] - '0') - 49][Integer.parseInt(s+chars[0]+chars[1])-1]==2){
                    sender.sendPrivateMsg(groupMsg,"这个点被下了，换个地方吧");
                    return;
                }
                boolean bai = wzq.bai((chars[2] - '0') - 49,Integer.parseInt(s+chars[0]+chars[1])-1,  new File("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg"));
                map.put(groupMsg.getAccountInfo().getAccountCode(),wzq.qiPan);
                if (bai){
                    MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                    sender.sendPrivateMsg(groupMsg,msg);
                    sender.sendPrivateMsg(groupMsg,"你赢啦");
                    map.remove(groupMsg.getAccountInfo().getAccountCode());
                    return;
                }
                wzq.setQiPan(map.get(groupMsg.getAccountInfo().getAccountCode()));
                boolean hei = wzq.hei(new File("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg"));
                map.put(groupMsg.getAccountInfo().getAccountCode(),wzq.qiPan);
                if (hei){
                    MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                    sender.sendPrivateMsg(groupMsg,msg);
                    sender.sendPrivateMsg(groupMsg,"我赢啦");
                    return;
                }
                MessageContent msg = builder.imageLocal("D:\\steam\\steamapps\\common\\wuzi\\" + groupMsg.getAccountInfo().getAccountCode() + ".jpg").build();
                sender.sendPrivateMsg(groupMsg,msg);
                sender.sendPrivateMsg(groupMsg,"请落子");
            }
        }
    }


}
